#ifndef _MATH_CORE_H_
#define _MATH_CORE_H_

#include "../sleipner_config.h"
#ifdef _PLATFORM_SDL
#include <math.h>
#endif

#define	MATH_2PI		6.28318f
#define	MATH_PI			3.14159f
#define	MATH_HALF_PI	1.57079f
#define	DEG_TO_RAD		0.01745f
#define RAD_TO_DEG		57.2957f

typedef int FAKE_FP;
class	MathFunc
{
public:
	static 	bool		IsPow2(int v)
	{
		return (v & (v - 1)) == 0;
	}
	static	int			MakePow2(int v)
	{
		for (int i=1; i<32; i++)
		{
			int		Pow2 = 0x01 << i;
			if ( v <= Pow2) return Pow2;
		}
		return 0;
	}
	/* bezier curve given by:
	B(t) = (1-t)2P0 + 2t(1-t)P1 + t2P2 from t = 0 to t = 1
	*/
	static void			ComputerBezier(const math::Vec2& A, const math::Vec2& B, const math::Vec2& C, float t, math::Vec2& Out)
	{
		float	t1 = (t*t);
		float	t2 = (2*t*(1-t));
		float   t3 = ((1-t)*(1-t));
		Out.x = (t1* A.x) + (t2 * B.x) + (t3 * C.x);
		Out.y = (t1* A.y) + (t2 * B.y) + (t3 * C.y);
	}

	// Fake Fixed point to avoid endian issues.
	// replace this with a real Fixed Point later?
	static const int		MUL_FACTOR		= 1000;	// 3 digits of data so 32.423 would be equivalent to 32432
	static float				ToFP(FAKE_FP		Value)
	{
		int	ND = cpu_to_le32(Value);
		return float(ND) / float (MUL_FACTOR);		
	}
	static FAKE_FP					FromFP(float		Value)
	{
		return cpu_to_le32(FAKE_FP(Value * float (MUL_FACTOR)));		
	}
		
};

#endif // _MATH_CORE_H_